// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GlobalMapView.xaml.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Navigation.Visualization
{    
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Input;
    using System.Windows.Media;

    using Microsoft.Robotics.Hardware.DAC;
    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// Interaction logic for GlobalMapView.xaml
    /// </summary>
    public partial class GlobalMapView : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the GlobalMapView class
        /// </summary>
        public GlobalMapView()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Handles the mouse wheel event to zoom in/out 
        /// </summary>
        /// <param name="args">The mouse wheel arguments</param>
        protected override void OnPreviewMouseWheel(MouseWheelEventArgs args)
        {
            base.OnPreviewMouseWheel(args);
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {
                double minValue = Math.Min(this.parentDock.ActualWidth / this.globalMap.Frame.Size.Width, this.parentDock.ActualHeight / this.globalMap.Frame.Size.Height);
                const double MaxValue = 2;
                this.uiScaleSlider.Value += (args.Delta > 0) ? 0.05 : -0.05;
                if (this.uiScaleSlider.Value < minValue)
                {
                    this.uiScaleSlider.Value = minValue;
                }
                else if (this.uiScaleSlider.Value > MaxValue)
                {
                    this.uiScaleSlider.Value = MaxValue;
                }

                this.zoomable.RenderTransformOrigin = Mouse.GetPosition(this.zoomable);
            }
        }

        /// <summary>
        /// Handle mouse down event
        /// </summary>
        /// <param name="sender">The event sender</param>
        /// <param name="args">The event arguments</param>
        private void OnMouseDown(object sender, MouseButtonEventArgs args)
        {
            if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
            {
                ((GlobalMapModel)(this.DataContext)).MouseStart = Mouse.GetPosition(this.globalMap);
            }
        }

        /// <summary>
        /// Handle mouse up event
        /// </summary>
        /// <param name="sender">The event sender</param>
        /// <param name="args">The event arguments</param>
        private void OnMouseUp(object sender, MouseButtonEventArgs args)
        {
            if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
            {
                ((GlobalMapModel)(this.DataContext)).MouseEnd = Mouse.GetPosition(this.globalMap);
            }
        }

        /// <summary>
        /// Handle mouse move event by showing the map coordinates.
        /// </summary>
        /// <param name="sender">The event sender</param>
        /// <param name="args">The event arguments</param>
        private void OnMouseMove(object sender, MouseEventArgs args)
        {
            ((GlobalMapModel)(this.DataContext)).MouseMove = Mouse.GetPosition(this.globalMap);
        }
    }
}
